﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YouErYuanManage.Models;
using YouErYuanBLL;
using YouErYuanModels;
using CommonLib;

namespace YouErYuanManage.Controllers
{
    public class LoginController : Controller
    {
        TeacherBLL userbll = new TeacherBLL();
        ManageLoginLogBLL loginbll = new ManageLoginLogBLL();  

        public ActionResult LoginView()
        {
            return View();   
        }

        [HttpPost]
        public string CheckLogin()
        {
            string userName = Request["userName"];
            string password = Request["password"];
            if (string.IsNullOrEmpty(userName))
            {
                return CommonMethod.FormatJsonObj("22");
            }
            if (string.IsNullOrEmpty(password))
            {
                return CommonMethod.FormatJsonObj("33");
            }
            Teacher model = userbll.getTeacher(userName, password);
            if (model == null)
            {
                return CommonMethod.FormatJsonObj("2");//用户名或密码错误
            }
            else
            {
                //解密密码
                model.password = DESencodingTxt.DESDecoding(model.password);
                if (model.Type.Equals("2"))//表示为园长
                {
                    if (model.status.Equals("1"))
                    {
                        this.HttpContext.Session["user"] = model;
                        this.HttpContext.Session.Timeout = 20;
                        //记录登录日志
                        loginbll.AddLoginLog(model.Id, "1", "登录成功");
                        return CommonMethod.FormatJsonObj("1");//授权登录,登录成功
                    }
                    else if (model.status.Equals("0"))
                    {
                        //记录登录日志
                        loginbll.AddLoginLog(model.Id, "1", "已被禁用账号尝试登录");
                        return CommonMethod.FormatJsonObj("4");//账号被禁用
                    }
                    else
                    {
                        //记录登录日志
                        loginbll.AddLoginLog(model.Id, "1", "无效账户");
                        return CommonMethod.FormatJsonObj("5");//无效账户
                    }
                }
                else if (model.Type.Equals("3"))//管理员
                {
                    this.HttpContext.Session["user"] = model;
                    this.HttpContext.Session.Timeout = 20;
                    //记录登录日志
                    loginbll.AddLoginLog(model.Id, "1", "管理员登录成功");
                    return CommonMethod.FormatJsonObj("6");//管理员登录
                }
                else
                {
                    //记录登录日志
                    loginbll.AddLoginLog(model.Id, "1", "没有登录权限");
                    return CommonMethod.FormatJsonObj("3");//没有登录权限
                }
            }
        }

        // 退出系统
        [YouErYuanManage.Filter.IsLogin]
        public string Exit()
        {
            //记录日志
            Teacher model = HttpContext.Session["user"] as Teacher;
            if (model != null)
            {
                if (model.Type.Equals("3"))
                {
                    //记录退出日志
                    loginbll.AddLoginLog(model.Id, "0", "管理员退出系统");
                }
                else
                {
                    //记录退出日志
                    loginbll.AddLoginLog(model.Id, "0", "退出系统");
                }
            }
            HttpContext.Session["user"] = null;
            return CommonMethod.FormatJsonObj("1");
        }

    }
}